package com.useeinfo.oa.modules.maintaining.thread.task;

import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.PlanExecuteTimeBiz;
import com.useeinfo.oa.modules.maintaining.entity.PlanExecuteTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class PlanTask implements Runnable {
    @Autowired
    private PlanExecuteTimeBiz planExecuteTimeBiz;

    private final static Logger logger = LoggerFactory.getLogger(PlanTask.class);

    private volatile MaintainOrderBiz maintainOrderBiz;

    private List<PlanExecuteTime> planExecuteTimes;

    public PlanTask(MaintainOrderBiz maintainOrderBiz, List<PlanExecuteTime> planExecuteTimes) {
        this.maintainOrderBiz = maintainOrderBiz;
        this.planExecuteTimes = planExecuteTimes;
    }

    @Override
    public void run() {
        try {
            //TODO,在此调用计划生成工单，开始时间结束时间和提醒时间请查看planExecuteTime
            planExecuteTimes.forEach(planExecuteTime -> {
                Date StartDate = StringConverters.ToDatePattern(planExecuteTime.getStartTime(), "yyyy-MM-dd HH:mm");
                Date endDate = StringConverters.ToDatePattern(planExecuteTime.getEndTime(), "yyyy-MM-dd HH:mm");
                maintainOrderBiz.makeMaintainOrder(planExecuteTime.getPlannerOrder(), StartDate, endDate,1,planExecuteTime);
            });
        } catch (Exception e) {
            logger.info("计划任务线程失败");
        }
    }
}
